package com.example.myprojectoffeixue;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class JidingHeActivity extends Activity implements OnClickListener {
    Map <Integer,String >map;
    private MyApplication m;
    private boolean flag1;
    HongWaiSB h;
    private controllerList c;
    private Vibrator vibrator;
    private ImageView bt0;
    private ImageView bt1;
    private ImageView bt2;
    private ImageView bt3;
    private ImageView bt4;
    private ImageView bt5;
    private ImageView bt6;
    private ImageView bt7;
    private ImageView bt8;
    private ImageView bt9;
    private ImageView btashang;
    private ImageView btaxia;
    private ImageView btaqueren;
    private ImageView btafanhui;
    private ImageView btazuo;
    private ImageView btayou;
    private ImageView btapindaojia;
    private ImageView btapindaojian;
    private ImageView btayinliangjia;
    private ImageView btayinlianjian;
    private ImageView btadianyuan;
    private ImageView btabaocun;
    private ImageView bacaidan;
    private ImageView badaoshi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studyji_ding_he);
        info();
    }
    private void info() {
        ImageView iv = (ImageView) findViewById(R.id.a0baocun);
        iv.setVisibility(View.GONE);

        LinearLayout ivfanhui = (LinearLayout) findViewById(R.id.iv_yaokongqi);

        ivfanhui.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        Intent it = getIntent();
        c = (controllerList) it.getSerializableExtra("controllerList");

        if (c.selfLearning == 1) {

            flag1 = true;

            map = new HashMap<Integer,String>();

            for (int i = 0; i < c.controllerDetail.size(); i++) {
                int codeValue = c.controllerDetail.get(i).button;
                String code = c.controllerDetail.get(i).originalCode;
                map.put(codeValue, code);


            }

        }


        m = (MyApplication) getApplication();
        h = new HongWaiSB();
        h.cmd = "1012";
        h.extra = new Extra();

        h.status = "04";
        if (!flag1) {
            h.status = "01";

        }

            h.extra.type = c.kind;
            h.mac=m.mac_ykq;
            h.ieee = m.ieee_ykq;
            h.port = m.port_ykq;


        bt0 = (ImageView) findViewById(R.id.a00);
        bt0.setOnClickListener(this);
        bt1 = (ImageView) findViewById(R.id.a01);
        bt1.setOnClickListener(this);
        bt2 = (ImageView) findViewById(R.id.a02);
        bt2.setOnClickListener(this);
        bt3 = (ImageView) findViewById(R.id.a03);
        bt3.setOnClickListener(this);
        bt4 = (ImageView) findViewById(R.id.a04);
        bt4.setOnClickListener(this);
        bt5 = (ImageView) findViewById(R.id.a05);
        bt5.setOnClickListener(this);
        bt6 = (ImageView) findViewById(R.id.a06);
        bt6.setOnClickListener(this);
        bt7 = (ImageView) findViewById(R.id.a07);
        bt7.setOnClickListener(this);
        bt8 = (ImageView) findViewById(R.id.a08);
        bt8.setOnClickListener(this);
        bt9 = (ImageView) findViewById(R.id.a09);
        bt9.setOnClickListener(this);
        btashang = (ImageView) findViewById(R.id.a0shang);
        btashang.setOnClickListener(this);
        btaxia = (ImageView) findViewById(R.id.a0xia);
        btaxia.setOnClickListener(this);
        btaqueren = (ImageView) findViewById(R.id.a0queren);
        btaqueren.setOnClickListener(this);
        btafanhui = (ImageView) findViewById(R.id.a0fanhui);
        btafanhui.setOnClickListener(this);
        btazuo = (ImageView) findViewById(R.id.a0zuo);
        btazuo.setOnClickListener(this);
        btayou = (ImageView) findViewById(R.id.a0you);
        btayou.setOnClickListener(this);
        btapindaojia = (ImageView) findViewById(R.id.a0pindaojia);
        btapindaojia.setOnClickListener(this);
        btapindaojian = (ImageView) findViewById(R.id.a0pindaojian);
        btapindaojian.setOnClickListener(this);
        btayinliangjia = (ImageView) findViewById(R.id.a0yinliangjia);
        btayinliangjia.setOnClickListener(this);
        btayinlianjian = (ImageView) findViewById(R.id.a0yiliangjian);
        btayinlianjian.setOnClickListener(this);

        btadianyuan = (ImageView) findViewById(R.id.a0dianyuan);
        btadianyuan.setOnClickListener(this);
        btabaocun = (ImageView) findViewById(R.id.a0baocun);
        btabaocun.setOnClickListener(this);
        bacaidan = (ImageView) findViewById(R.id.a0caidan);
        bacaidan.setOnClickListener(this);
        badaoshi = (ImageView) findViewById(R.id.a0daoshi);
        badaoshi.setOnClickListener(this);
        vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
    }



    public void send(int i) {

        String l = "";
        if (flag1) {
            if (map.containsKey(i)) {
                h.extra.code = map.get(i);
                l = JSON.toJSONString(h);

                if(!Gateway.MacSocke.containsKey(m.mac_ykq)){
                    if (m.flag3) {
                        m.c.send(l);
                    }else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }

                }else{
                    String cmd = GatewayRequestUtil.createInfraredForwardingStr3(m.ieee_ykq, m.port_ykq,map.get(i));
                    SendUtil.Mac = m.mac_ykq;
                    SendUtil.cmd = cmd;
                    SendUtil.hand.sendEmptyMessage(0);
                }
                vibrator.vibrate(new long[]{0, 300}, -1);
            } else {
                HttpConnection.showToast(getBaseContext(),"你还没有学习本键");

            }


        } else {
            h.extra.codeIndex = c.controllerDetail.get(0).num + "";
            h.extra.codeValue = "" + i;
            l = JSON.toJSONString(h);

            if(!Gateway.MacSocke.containsKey(m.mac_ykq)){
                if (m.flag3) {
                    m.c.send(l);}else {

                    HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                }

            }else{
                Log.d("内网发送命令","内网"+m.ieee_ykq+"--"+m.port_ykq+"--"+m.type+"--"+c.controllerDetail.get(0).num+"--"+h.extra.codeValue);
                String cmd = GatewayRequestUtil.createInfraredForwardingStr(m.ieee_ykq, m.port_ykq,m.type,c.controllerDetail.get(0).num,h.extra.codeValue);
                SendUtil.Mac = m.mac_ykq;
                SendUtil.cmd = cmd;
                SendUtil.hand.sendEmptyMessage(0);
            }

            vibrator.vibrate(new long[]{0, 300}, -1);

        }
    }














    @Override
    public void onClick(View v) {
        String l = "";

        switch (v.getId()) {

            case R.id.a00:


                send(23);
                break;


            case R.id.a0daoshi:

                send(21);
                break;

            case R.id.a01:
                send(3);
                break;

            case R.id.a02:
                send(5);
                break;

            case R.id.a03:
                send(7);
                break;

            case R.id.a04:
                send(9);
                break;

            case R.id.a05:
                send(11);
                break;

            case R.id.a06:
                send(13);
                break;

            case R.id.a07:
                send(15);

                break;

            case R.id.a08:
                send(17);
                break;

            case R.id.a09:
                send(19);
                break;

            case R.id.a0caidan:
                send(45);
                break;

            case R.id.a0dianyuan:
                send(1);

                break;

            case R.id.a0fanhui:
                send(25);
                break;


            case R.id.a0pindaojia:
                send(41);
                break;
            case R.id.a0pindaojian:
                send(43);

                break;
            case R.id.a0yinliangjia:
                send(37);
                break;
            case R.id.a0yiliangjian:
                send(39);
                break;
            case R.id.a0shang:
                send(27);
                break;
            case R.id.a0xia:
                send(35);
                break;
            case R.id.a0zuo:
                send(29);

                break;
            case R.id.a0you:
                send(33);

                break;
            case R.id.a0queren:
                send(31);

                break;


        }

    }

}
